package com.czy.owner.ui.userinfo;

import android.graphics.Bitmap;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.global.Constants;
import com.czy.owner.net.utils.MyLoadView;
import com.czy.owner.ui.BaseActivity;
import butterknife.BindView;

/**
 * Created by wangyang on 2016/7/20 0020.
 * 关于我们
 */

public class AboutUsActivity extends BaseActivity {
    @BindView(R.id.webview)
    WebView webview;
    private String session;
    private String url;
    private String postDate;
    private MyLoadView myLoadView;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_webview;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getToolbarTitle().setText(R.string.about_us);
        session = UserHelper.getInstance().getSessionInfoModel(this).getSession();
        myLoadView = new MyLoadView(AboutUsActivity.this, "正在加载");
        webview.setWebViewClient(new MyWebViewClient());
        url = Constants.APP_API_BASE + Constants.OWN_ABOUT;
        WebSettings ws = webview.getSettings();
        ws.setJavaScriptEnabled(true);
        ws.setUseWideViewPort(false);  //将图片调整到适合webview的大小
        ws.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK); //关闭webview中缓存
        postDate = "session=" + session;
        webview.postUrl(url, postDate.getBytes());
    }

    @Override
    protected void updateViews() {

    }


    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.postUrl(url, postDate.getBytes());
            return super.shouldOverrideUrlLoading(view, url);

        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            myLoadView.CancleLoadView();
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

            myLoadView.ShowLoadView();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        webview.destroy();
    }

}
